import sysConfig from 'src/api/sysConfig';
import { dispatch } from 'src/redux/store';
import { finishWaiting, startWaiting, pushNotification } from 'src/redux/uiSlice';

type PostAuthRequest = any


export const getSysBank = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await sysConfig.getSysBank(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};

export const getNotice = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await sysConfig.getNotice(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const getNoticeInfo = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await sysConfig.getNoticeInfo(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const getMyPushMsg = async (data: PostAuthRequest) => {
    try {
        const res = await sysConfig.getMyPushMsg(data);
        if (res.data.success) {
            const lastNotifications = localStorage.getItem('notification')?.split(',');
            const newNotifications = res.data.list.filter((v: { FID: string; }) => {
                if (!lastNotifications) return true;
                let id = v.FID.toString()
                return !lastNotifications.includes(id);
            });
            // if (newNotifications.filter((v: { type: Type; }) => Object.values(Type).includes(v.type)).length > 0) {
            //     const processedNotification = handleNotification(newNotifications);
            //     notify(processedNotification);
            // }

            newNotifications.forEach((item: any) => {
                if (!item.FState) {
                    dispatch(pushNotification([item]));
                    const moby = require('src/audio/notify.wav')
                    const audio = new Audio(moby);
                    audio.play()
                }
            })
            localStorage.setItem('notification', res.data.list.map((v: { FID: any; }) => v.FID).join());
        } else localStorage.removeItem('notification');
        // return data.data = res.data
    } finally {
    }
};
export const updatePushMsg = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await sysConfig.updatePushMsg(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
}; 
